package com.dabbo.starship.cell;
import java.rmi.RemoteException;
import java.util.HashMap;

import com.dabbo.starship.interfaces.Block;
import com.dabbo.starship.interfaces.Cell;
import com.dabbo.starship.interfaces.CellLocationInterface;
import com.dabbo.starship.interfaces.ItemInterface;
import com.dabbo.starship.shared.CellLocation;

public class CellDefault extends java.rmi.server.UnicastRemoteObject implements Cell {

	private static final long serialVersionUID = -2984274291065745554L;

	protected boolean 							activated 		= false;
	protected boolean 							cargoAllowed 	= false;
	protected int								storageLimit	= 0;
	protected boolean 							passable 		= false;
	protected int 								powerOutput 	= 0;
	protected boolean 							doorSecure 		= false;
	protected String 							doorAccessCode 	= null;
	protected int 								cellType 		= Cell.CELL_DEFAULT;
	protected int 								thrust 			= 0;
	protected int 								consoleMode 	= 0;
	protected CellLocation 						thisLocation	= null;
	protected Block								parentBlock		= null;
	protected String							commandCode		= "";
	protected int								damage			= 0;
	protected HashMap<Integer, ItemInterface>	items			= new HashMap<Integer, ItemInterface>();
	
	public CellDefault(Block parent) throws RemoteException {
		this.parentBlock = parent;
		cellType = Cell.CELL_DEFAULT;
	}

	public int getCellType() throws RemoteException {
		return this.cellType;
	}
	
	public void tick() throws RemoteException {
		// Immune to damage by default.
		damage = 0;
	}
	
	public void activate() throws RemoteException {
	}

	public void deActivate() throws RemoteException {
	}

	public int getPowerOutput() throws RemoteException {
		return this.powerOutput;
	}

	public boolean isActive() throws RemoteException {
		return this.activated;
	}

	public boolean isCargoAllowed() throws RemoteException {
		return this.cargoAllowed;
	}

	public void powerFailue() throws RemoteException {
		this.activated = false;
		this.powerOutput = 0;
	}

	public boolean isPassable() throws RemoteException {
		return this.passable;
	}

	public void setExit(Block parentBlock, CellLocationInterface parentCell, Block linkedBlock, CellLocationInterface linkedCell, boolean callBack) throws RemoteException {
	}
	
	public boolean getDoorSecure() throws RemoteException {
		return this.doorSecure;
	}

	public void setDoorSecure(boolean secureMode, boolean stopCallBack) throws RemoteException {
	}

	public String getDoorAccessCode() throws RemoteException {
		return this.doorAccessCode;
	}

	public void setDoorAccessCode(String newDoorAccessCode) throws RemoteException {
		this.doorAccessCode = newDoorAccessCode;
	}

	public void setThrust(int thrust) throws RemoteException {
	}

	public int getThrust() throws RemoteException {
		return 0;
	}

	public int getConsoleMode() {
		return this.consoleMode;
	}

	public void setConsoleMode(int consoleMode) {
		this.consoleMode = consoleMode;
	}

	public void setLocation(CellLocation cellLocation) throws RemoteException {
		this.thisLocation = cellLocation;
	}

	public CellLocation getLocation() throws RemoteException {
		return this.thisLocation;
	}

	public String getCommandCode() throws RemoteException {
		return this.commandCode;
	}

	public boolean setCommandCode(String commandCode, String oldCommandCode) throws RemoteException {
		if (this.commandCode.equals("") || (this.commandCode.equals(oldCommandCode))) {
			this.commandCode = commandCode;
			return true;
		}
		return false;
	}

	public Block getExitBlock() throws RemoteException {
		return null;
	}

	public CellLocationInterface getExitCell() throws RemoteException {
		return null;
	}

	public int getBatteryCharge() throws RemoteException {
		return 0;
	}

	public void setBatteryCharge(boolean changing) throws RemoteException {
	}

	public boolean isCharging() throws RemoteException {
		return false;
	}

	public int getDamage() throws RemoteException {
		return this.damage;
	}

	public void setDamage(int damage) throws RemoteException {
		this.damage = damage;
	}

	public Block getParent() throws RemoteException {
		return this.parentBlock;
	}

	@Override
	public int getStorageLimit() throws RemoteException {
		return this.storageLimit;
	}

	@Override
	public int getAvailableStorage() throws RemoteException {
		int usedStorage = 0;
		for (int i = 0; i < items.size(); i++) {
			usedStorage = usedStorage + items.get(i).getItemSize();
		}
		return (this.storageLimit - usedStorage);
	}

	@Override
	public boolean addItem(ItemInterface item) throws RemoteException {
		if (items.containsKey(item.hashCode())) {
			// If item already in list, fail request.
			return false;
		}
		if (getAvailableStorage() >= item.getItemSize()) {
			items.put(item.hashCode(), item);
			return true;
		}
		// Not enough storage to store object, return fail.
		return false;
	}

	@Override
	public boolean removeItem(int element) throws RemoteException {
		if (items.containsKey(element)) {
			items.remove(element);
			return true;
		}
		return false;
	}

	@Override
	public boolean isCargoHere() throws RemoteException {
		if (items.size() > 0) {
			return true;
		}
		return false;
	}

}